// Module to control the application lifecycle and the native browser window.
const { app, BrowserWindow, protocol } = require("electron");
const path = require("path");
const url = require("url");

// Create the native browser window.
function createWindow() {
    const mainWindow = new BrowserWindow({
        width: 800,
        height: 600,
        // Set the path of an additional "preload" script that can be used to
        // communicate between node-land and browser-land.
        webPreferences: {
            preload: path.join(__dirname, "preload.js"),
        },
    });

    // In production, set the initial browser path to the local bundle generated
    // by the Create React App build process.
    // In development, set it to localhost to allow live/hot-reloading.
    const appURL = app.isPackaged
        ? url.format({
            pathname: path.join(__dirname, "index.html"),
            protocol: "file:",
            slashes: true,
        })
        : "http://localhost:3000";
    mainWindow.loadURL(appURL);

    // Automatically open Chrome's DevTools in development mode.
    // if (!app.isPackaged) {
        mainWindow.webContents.openDevTools();
    // }
}

// Setup a local proxy to adjust the paths of requested files when loading
// them from the local production bundle (e.g.: local fonts, etc...).
function setupLocalFilesNormalizerProxy() {
    protocol.registerHttpProtocol(
        "file",
        (request, callback) => {
            const url = request.url.substr(8);
            callback({ path: path.normalize(`${__dirname}/${url}`) });
        },
        (error) => {
            if (error) console.error("Failed to register protocol");
        },
    );
}

// This method will be called when Electron has finished its initialization and
// is ready to create the browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
    createWindow();
    setupLocalFilesNormalizerProxy();

    app.on("activate", function () {
        // On macOS it's common to re-create a window in the app when the
        // dock icon is clicked and there are no other windows open.
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    });
});

// Quit when all windows are closed, except on macOS.
// There, it's common for applications and their menu bar to stay active until
// the user quits  explicitly with Cmd + Q.
app.on("window-all-closed", function () {
    if (process.platform !== "darwin") {
        app.quit();
    }
});

const { autoUpdater } = require("electron-updater");

autoUpdater.setFeedURL({
    provider: "generic",
    url: "https://webdownload.zhizisupplier.top/", // 替换为你的七牛云 Bucket 外链根目录
});


let mainWindow, isChecking = false;

// 配置自动更新
autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;

app.whenReady().then(() => {
    mainWindow = new BrowserWindow({ /* ... */ });

    // 首次检查
    checkUpdate();

    // 每6小时检查
    setInterval(checkUpdate, 6 * 60 * 60 * 1000);

    // 窗口焦点事件
    mainWindow.on('focus', checkUpdate);
});

// 封裝检查逻辑
function checkUpdate() {
    if (isChecking || !mainWindow) return;
    isChecking = true;
    autoUpdater.checkForUpdatesAndNotify();
}

// 更新事件监听
autoUpdater.on('update-not-available', () => {
    isChecking = false;
});

autoUpdater.on('error', (err) => {
    isChecking = false;
    console.error('更新检查失败:', err);
});